<?php session_start(); ?>
<html>
    <head>
        <title>tan</title>
        <link href="public/css/css.css" rel="stylesheet" type="text/css">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
        <div id="wrapper">
            <div id="baner">

                <img src="public/image/logo.png">

            </div>
            <div id='boder_menu'>
                <ul>

                    <li><a href="#">HOME</a></li>
                    <li><a href="#">CONTENT</a></li>
                    <li><a href="#">FORUMS</a></li>
                    <li><a href="#">CONTACT</a></li>
                </ul>
            </div>


            <div id="col_left">


                <div class="logo">

                    <?php
                    require_once 'model/db2.php';
                    require_once 'model/works.php';
                    require_once 'model/paginationdemo.php';
//        
                    $works = new Works();
                    $paginations = new Pagination();
                    ?>      
                    <?php
                    if (!isset($_GET["page"]))
                        $page = 1;
                    else
                        $page = $_GET["page"];
                    $list = $works->select_index($page);
                    $total = $works->countWords();
                    echo $paginations->Display($page, $total, null, "login.php");
                    ?>
                    <br> 
                     <?php if(isset($_SESSION["id"])): ?>
                    <div id="logout"><a href="logout.php"><b>Đăng Xuất</b></a></div>
                    <?php endif;?>
                    <table border="1px" align="center">
                        <tr class="hang">
                            <td><b>id</b></td>
                            <td><b>name</b></td>
                            <td><b>description</b></td>
                            <td><b>posted</b></td>
                            <td><b>Edit</b></td>
                            <td><b>Image</b></td>
                        </tr>
                        <?php foreach ($list as $item): ?>
                            <tr>
                                <td class="echo"><?php echo$item['id']; ?></td>
                                <td class="bgtable"><a href="<?php echo$item['url']; ?>" style="text-decoration: none">
                                        <?php echo$item['name']; ?></a></td>                                            
                                <td class="bgtable"><?php echo substr($item['description'], 0, 100); ?></td>
                                <td class="echo"><?php $mydate = getdate($item['posted']); ?> 
                                    <?php echo "$mydate[mday] $mydate[month], $mydate[year]"; ?></td>
                                <?php if(isset($_SESSION["id"]) && $_SESSION["id"]==0 ): ?>
                                <td class="echo"> <a href="upload.php?id=<?php echo $item['id']; ?>">Edit</a>
                                     <a href="view.php?id=<?php echo $item['id']; ?>">View</a><br>
                                <td class="echo"><img src="<?php echo $item['image']; ?>" width="200px"></td>
                                <td class="echo"> <a href="del.php?id="<?php $item['id']; ?> ></a></td>
                                <?php endif; ?>
 <td class="echo"><a href="view.php?id=<?php echo $item['id']; ?>">View</a><br></td>
                            <?php endforeach; ?>
                        </tr>
                    </table>
                    <br>
                    <?php echo $paginations->Display($page, $total, null, "login.php"); ?>
                    <br>

                </div>



            </div>
            <form method="post"      enctype="multipart/form-data">    
                <label for="file">Filename:</label> 
                <input type="file" name="file" id="file"><br>
                <input type="submit" name="submit" value="Submit">  
            </form>

            <?php
            if (isset($_POST['submit'])) {
                $allowedExts = array("gif", "jpeg", "jpg", "png");
                $temp = explode(".", $_FILES["file"]["name"]);
                $extension = end($temp);
                if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 5000000) && in_array($extension, $allowedExts)) {
                    if ($_FILES["file"]["error"] > 0) {
                        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                    } else {
                        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
                        echo "Type: " . $_FILES["file"]["type"] . "<br>";
                        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
                        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

                        if (file_exists("F:\images/" . $_FILES["file"]["name"])) {
                            echo "Error: " . $_FILES["file"]["name"] . " already exists. " . "<br>";
                        } else {
                            move_uploaded_file($_FILES["file"]["tmp_name"], "F:\images/" . $_FILES["file"]["name"]);
                            echo "Stored in: " . "C:\Program Files (x86)\VertrigoServ\www\hvtan-web2\public\image" . $_FILES["file"]["name"] . "<br>";
                        }
                        echo"Upload to: " . "F:\images/" . $_FILES["file"]["name"];
                    }
                } else {
                    echo "Invalid file";
                }
            }
            ?>

<!--            <footer>
                <div id="boder">
                    <table class="o">
                        <tr>
                            <td>HÀ VAN TAN</td>
                            
                        </tr>
                    </table>
            </footer>-->
    </body>
</html>
